import Mock from "mockjs";
import { User } from "./db";
import { URL } from "./index";

const base = "http://localhost:3000";

Mock.mock(base + URL.ALOGIN, "post", (opt) => {
    let req = JSON.parse(opt.body);
    // console.log(`User: ${typeof User}--- ${JSON.stringify(User)}`);
    let UserCount = User.length;

    for (let i = 0; i < UserCount; i++) {
        if (User[i].mobilePhone !== req.mobilePhone) {
            // 遍历完 User，仍然查询不到 phone，返回错误
            if (i === UserCount - 1) {
                return {
                    status:  202,
                    message: "手机号码尚未注册"
                };
            }
        } else {
            if (User[i].password !== req.password) {
                return {
                    status:  201,
                    message: "密码不正确"
                };
            } else {
                return {
                    status:  200,
                    message: "",
                    data:    {
                        access_token: User[i].access_token,
                        uid:          User[i].uid
                    }
                };
            }
        }
    }
});

Mock.mock(base + URL.AGETUSERINFO, "get", () => {
    return {
        status:  200,
        message: "",
        data:    User[0]
    };
   /* return setTimeout(() => {
        return {
            status:  200,
            message: "",
            data:    User[0]
        };
    }, 0);*/
});
